package com.spboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spboot.entity.*;
import com.spboot.page.PageInfo;
import com.spboot.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author guocb
 * @since 2020-04-27
 */
@Controller
@RequestMapping("/like")
public class LikeController {
    @Autowired
    LikeService likeService;
    @Autowired
    ShoppingcartService shoppingcartService;
    @Autowired
    ModdletableService moddletableService;
    @Autowired
    EditionService editionService;
    @Autowired
    ColorService colorService;
    @Autowired
    PriceService priceService;
    @Autowired
    InformationService informationService;

    /**
     * 添加喜欢
     * @param informationid
     * @param userid
     * @return
     */
    @RequestMapping("insert")
    public String insert(Long informationid,Long userid,Long editionid,Long colorid,Long priceid){
        //1.先查询数据库是否有这个颜色版本价格
        Integer sum=likeService.selectsum(informationid, editionid, colorid, priceid);
        if (sum == 1){
            //2.如果有查询tableid
            Integer id=likeService.selectid(informationid, editionid, colorid, priceid);
            //3.添加购物车
            int insert=likeService.tianjia(informationid, userid,(long) id);

        }else{
            //2.如果数据库没有这个颜色版本价格添加一个
            int tableid=likeService.inserttable(informationid, editionid, colorid, priceid);
            //3.查询tableid
            Integer id=likeService.selectid(informationid, editionid, colorid, priceid);
            //4.添加购物车
            int insert=likeService.tianjia(informationid, userid,(long) id);
        }
      return "redirect:/information/purchase?informationid="+informationid;

    }

    /**
     * 查询喜欢
     * @param pageno
     * @param userid
     * @param model
     * @return
     */
    @RequestMapping("select")
    public String likeall(@RequestParam(defaultValue = "1", required = false) Long pageno,Integer userid, Model model){
        QueryWrapper<Like> queryWrapper = new QueryWrapper<>();
        IPage<Like> pageInfo = new PageInfo<>(pageno, 6);
        pageInfo = (PageInfo<Like>) likeService.likeall(userid,pageInfo, queryWrapper);

        if(userid!=null){
            model.addAttribute("selectl",pageInfo);
            //右上购物车查询
            model.addAttribute("shoplist",shoppingcartService.ssum(userid));
            model.addAttribute("select",shoppingcartService.selectall(userid));
            model.addAttribute("shu",shoppingcartService.ssum(userid));
            model.addAttribute("zong",shoppingcartService.zong(userid));
            return "like";
        }

            model.addAttribute("selectl",pageInfo);
            return "like";
    }

    /**
     * 删除喜欢
     * @param userid
     * @param informationid
     * @return
     */
    @RequestMapping("delete")
    public String delete(Long userid,Long informationid){
        Long delete = likeService.delete(informationid,userid);
        if(delete==1){
            return "select";
        }
        return "/";
    }
}
